﻿
namespace LS.Nc6.Application.System
{
    /// <summary>
    /// 提供用户登录管理服务
    /// </summary>
    public class SysUserService : BaseService<SysUserEntity>, ISysUserService, ITransient
    {
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public async Task<SysUserEntity> Login(string name, string pwd)
        {
            return await DbContext.Instance.Queryable<SysUserEntity>().SingleAsync(it => it.UserName == name && it.UserPwd ==pwd);
        }
        /// <summary>
        /// 用户是已存在
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        public bool ExistAccount(string account)
        {
            //string sql = "select top 1 id from sys_user where UserName = @UserName";
            //return SqlHelper.Query<SysUserEntity>(sql, new { UserName = account }).Count() > 0;
            return false;
        }
        public async Task<List<SysUserRoleEntity>> GetRoleUser(long userId)
        {

            return await DbContext.Instance.Queryable<SysUserRoleEntity>().LeftJoin<SysRoleEntity>((a, b) => a.RoleCode == b.RoleCode).Where((a, b) => a.UserId == userId).ToListAsync();
             
        }

        public  async Task<int> InsertUser(SysUserEntity model, List<SysUserRoleEntity> userroleList, List<SysOrganizeUserEntity> userorgList)
        {

            try
            {
                DbContext.Instance.BeginTran();
                var userid = await DbContext.Instance.Insertable(model).ExecuteReturnBigIdentityAsync();
                if (userroleList != null && userroleList.Count > 0)
                {
                    List<SysUserRoleEntity> listrole = new List<SysUserRoleEntity>();
                    userroleList.ForEach(a =>
                    {
                        listrole.Add(new SysUserRoleEntity { RoleCode = a.RoleCode, UserId = userid });
                    });
                    await DbContext.Instance.Insertable(listrole).ExecuteCommandAsync();
                }
                DbContext.Instance.CommitTran();
                return 1;
            }
            catch (Exception)
            {
                DbContext.Instance.RollbackTran();
                throw;
            }
        }

        public async Task<int> UpdateUser(SysUserEntity model, List<SysUserRoleEntity> userroleList, List<SysOrganizeUserEntity> userorgList)
        {
            try
            {
                DbContext.Instance.BeginTran();
                await DbContext.Instance.Updateable(model).ExecuteCommandAsync();
                if (userroleList != null && userroleList.Count > 0)
                {
                    await  DbContext.Instance.Deleteable<SysUserRoleEntity>().Where(a=>a.UserId ==model.Id).ExecuteCommandAsync();  
                    List<SysUserRoleEntity> listrole = new List<SysUserRoleEntity>();
                   
                    await DbContext.Instance.Insertable(userroleList).ExecuteCommandAsync();
                }
                DbContext.Instance.CommitTran();
                return 1;
            }
            catch (Exception)
            {
                DbContext.Instance.RollbackTran();
                throw;
            }
        }

        public async Task<int> UpdateEnable(long id, int enable)
        {
            
            return await DbContext.Instance.Updateable<SysUserEntity>().SetColumns(a => a.Isenable == enable.ParseToBool()).Where(a => a.Id == id).ExecuteCommandAsync();
        }

        public async Task<int> UpdatePwd(long id, string pwd)
        {
            return await DbContext.Instance.Updateable<SysUserEntity>().SetColumns(a => a.UserPwd == pwd).Where(a => a.Id == id).ExecuteCommandAsync();
        }

        public async Task<int> UpdateAvater(long id, string avatar)
        {
            
            return await DbContext.Instance.Updateable<SysUserEntity>().SetColumns(a => a.Avatar == avatar).Where(a => a.Id == id).ExecuteCommandAsync();
        }

        public async Task<int> AuditUser(string ids)
        {

            return await DbContext.Instance.Updateable<SysUserEntity>().SetColumns(a => a.UserStatus == 1).Where(a => ids.GetStringToLongArryNoNull().Contains(a.Id)).ExecuteCommandAsync();
        }

        public async Task<int> InsertAuditUser(List<SysUserRoleEntity> userroleList, List<SysOrganizeUserEntity> userorgList)
        {
          
            try
            {
                DbContext.Instance.BeginTran();
                
                if (userroleList != null && userroleList.Count > 0)
                {
                    List<long> listrole = new List<long>();
                    userroleList.ForEach(a =>
                    {
                        if (!listrole.Contains(a.UserId))
                        {
                            listrole.Add(a.UserId);
                        }
                    });
                    await DbContext.Instance.Deleteable<SysUserRoleEntity>().Where(a=> listrole.Contains(a.UserId)).ExecuteCommandAsync();
                    await DbContext.Instance.Insertable(userroleList).ExecuteCommandAsync();
                }
                DbContext.Instance.CommitTran();
                return 1;
            }
            catch (Exception)
            {
                DbContext.Instance.RollbackTran();
                throw;
            }
        }
    }
}